﻿using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration;
using ProjetoComperio.DataLayer.Configuration;
using ProjetoComperio.DataLayer.Entities;
//using DataLayer;
//using DataLayer.Configuration;

namespace ProjetoComperio.DataLayer.Infra
{
    public class DataContextFactory
    {
        /// <summary>
        /// Métoto Fabrica que cria um IComperioEntitiesDataModel
        /// </summary>
        /// <returns></returns>
        public static IComperioEntitiesDataModel FactoryDataContext()
        {

            var builder = new ModelBuilder();
            builder.Entity<AcademicPerson>().HasMany(ap => ap.Contributions).WithRequired(c => c.AcademicPerson);
            builder.Entity<AcademicPerson>().HasMany(ap => ap.KnowledgeAreas).WithMany(ka => ka.AcademicPeople);
            builder.Entity<AgeRange>().HasMany(ar => ar.EducationalITApplications).WithMany(ea => ea.AgeRanges);

            builder.Entity<Comment>().HasRequired(c => c.Subject).WithMany(s => s.Comments);

            builder.Entity<Subject>().HasMany(s => s.Comments).WithRequired(c => c.Subject).HasConstraint((c, s) => c.SubjectId == s.SubjectID);

            builder.Entity<Contribution>().HasMany(c => c.Subjects).WithRequired(s => s.Contribution).HasConstraint((s, c) => s.ContributionID == c.ContributionID);

            builder.Entity<Contribution>().HasMany(c => c.KnowledgeAreas).WithMany(ka => ka.Contrributions);

            builder.Entity<Contribution>().HasMany(c => c.ContributionItems).WithRequired(ci => ci.Contribution).HasConstraint((ci, c) => ci.ContributionID == c.ContributionID);

            builder.Entity<Contribution>().HasRequired(c => c.AcademicPerson).HasConstraint((c, ap) => c.AcademicPersonID == ap.PersonID);


            builder.Entity<Course>().HasMany(c => c.AcademicOrganizations).WithMany(ao => ao.Courses);
            builder.Entity<EducationalITApplication>().HasMany(ea => ea.Environments).WithMany(e => e.EducationalITApplications);
            builder.Entity<AcademicOrganization>().HasMany(ao => ao.KnowledgeAreas).WithMany(ka => ka.AcademicOrganizations);
            builder.Entity<AcademicOrganization>().HasRequired(ao => ao.AcademicOrganizationType).HasConstraint((aot, ao) => aot.AcademicOrganizationTypeID == ao.AcademicOrganizationTypeID);
            builder.Entity<AcademicOrganization>().HasMany(ao => ao.AcademicPeople).WithRequired(ap => ap.AcademicOrganization).HasConstraint((ao, ap) => ao.AcademicOrganizationID == ap.AcademicOrganizationID);
            builder.Entity<AcademicOrganization>().HasMany(ao => ao.EducationalITApplications).WithMany(ea => ea.AcademicOrganizations);


            builder.Entity<EducationLevel>().HasMany(el => el.People).WithRequired(p => p.EducationLevel).HasConstraint((p, el) => p.EducationLevelID == el.EducationLevelID);


            builder.Entity<Platform>().HasMany(p => p.Applications).WithMany(a => a.Platforms);
            builder.Entity<Proposal>();
            builder.Entity<AcademicPersonCV>();
            builder.Entity<Application>();

            builder.Configurations.Add(new AcademicCoordinatorConfiguration());
            builder.Configurations.Add(new AcademicPersonConfiguration());
            builder.Configurations.Add(new AcademicOrganizationConfiguration());
            builder.Configurations.Add(new AcademicOrganizationTypeConfiguration());
            builder.Configurations.Add(new AgeRangeConfiguration());
            builder.Configurations.Add(new ApplicationConfiguration());
            builder.Configurations.Add(new ArticleConfiguration());
            builder.Configurations.Add(new ContextConfiguration());
            builder.Configurations.Add(new ContributionConfiguration());
            builder.Configurations.Add(new ContributionItemConfiguration());

            builder.Configurations.Add(new EducationalTApplicationConfiguration());
            builder.Configurations.Add(new EnvironmentConfiguration());
            builder.Configurations.Add(new KnowledgeAreaConfiguration());
            builder.Configurations.Add(new NewContributionMemberConfiguration());
            builder.Configurations.Add(new PersonConfiguration());
            builder.Configurations.Add(new PlatformConfiguration());
            builder.Configurations.Add(new ProjectConfiguration());

            builder.Configurations.Add(new PublicationConfiguration());

            builder.Configurations.Add(new CourseConfiguration());
            builder.Configurations.Add(new StudentConfiguration());
            builder.Configurations.Add(new EventConfiguration());
            builder.Configurations.Add(new EducationLevelConfiguration());
            builder.Configurations.Add(new CommentConfiguration());
            builder.Configurations.Add(new SubjectConfiguration());
            builder.Configurations.Add(new ProposalConfiguration());
            builder.Configurations.Add(new AcademicPersonCVConfiguration());

            builder.ContainerName = "projetocomperio";
            var model = builder.CreateModel();
            return new ComperioDataModel(model);
        }

        public static void CreateDataBase()
        {

            var ctx = FactoryDataContext();
            if (ctx.DatabaseExists())
                ctx.DeleteDatabase();
            ctx.CreateDatabase();

        }

        public static void SetInitializer()
        {
            //Database.SetInitializer(new RecreateDatabaseIfModelChanges<ComperioDataModel>());
            Database.SetInitializer<ComperioDataModel>(null);
        }

    }
}
